package com.ds.dsm.view.config.form;

import com.ds.common.JDSException;
import com.ds.config.ListResultModel;
import com.ds.config.ResultModel;
import com.ds.dsm.repository.entity.FieldModuleView;
import com.ds.dsm.view.config.form.field.FieldFormInfo;
import com.ds.dsm.view.config.form.field.FieldHiddenInfo;
import com.ds.enums.db.MethodChinaName;
import com.ds.esd.custom.annotation.CustomAnnotation;
import com.ds.esd.custom.annotation.GridViewAnnotation;
import com.ds.esd.custom.api.annotation.APIEventAnnotation;
import com.ds.esd.custom.bean.MethodConfig;
import com.ds.esd.custom.buttonviews.annotation.ButtonViewsAnnotation;
import com.ds.esd.custom.form.CustomFormViewBean;
import com.ds.esd.custom.module.annotation.DynLoadAnnotation;
import com.ds.esd.custom.module.annotation.ModuleAnnotation;
import com.ds.esd.custom.nav.annotation.NavGroupViewAnnotation;
import com.ds.esd.custom.nav.annotation.TabsAnnotation;
import com.ds.esd.custom.nav.tab.buttonviews.NavButtonViewsViewAnnotation;
import com.ds.esd.dsm.DSMFactory;
import com.ds.esd.dsm.view.ViewEntityConfig;
import com.ds.esd.dsm.view.field.FieldFormConfig;
import com.ds.esd.dsm.view.field.FieldModuleConfig;
import com.ds.esd.tool.ui.component.tab.SideBarStatusType;
import com.ds.esd.tool.ui.enums.BarLocationType;
import com.ds.esd.tool.ui.enums.Dock;
import com.ds.web.util.PageUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping(path = "/dsm/view/config/form/")
@MethodChinaName(cname = "配置信息")
@TabsAnnotation(singleOpen = true)
@ButtonViewsAnnotation(barLocation = BarLocationType.top, sideBarStatus = SideBarStatusType.expand, barSize = "3em")
public class FormConfigView {
    @CustomAnnotation(pid = true, hidden = true)
    private String esdclass;
    @CustomAnnotation(pid = true, hidden = true)
    private String projectId;
    @CustomAnnotation(pid = true, hidden = true)
    private String viewInstId;
    @CustomAnnotation(pid = true, hidden = true)
    private String domainId;

    @CustomAnnotation(uid = true, hidden = true)
    private String methodName;

    public FormConfigView() {

    }

    public FormConfigView(CustomFormViewBean formConfig) {
        if (formConfig == null) {
            formConfig = new CustomFormViewBean();
        }
    }


    @RequestMapping(method = RequestMethod.POST, value = "FormInfoGroup")
    @NavGroupViewAnnotation(reSetUrl = "clear", saveUrl = "updateFormInfo", autoSave = true)
    @ModuleAnnotation(dock = Dock.fill, caption = "基础信息", imageClass = "spafont spa-icon-project")
    @ResponseBody
    public ResultModel<FormInfoGroup> getFormInfoGroup(String sourceClassName, String methodName, String domainId, String viewInstId) {
        ResultModel<FormInfoGroup> result = new ResultModel<FormInfoGroup>();
        return result;
    }


    @RequestMapping(method = RequestMethod.POST, value = "FieldFormList")
    @GridViewAnnotation
    @ModuleAnnotation(imageClass = "spafont spa-icon-c-comboinput", caption = "字段")
    @APIEventAnnotation(autoRun = true)
    @ResponseBody
    public ListResultModel<List<FieldFormInfo>> getFieldGrids(String sourceClassName, String methodName, String domainId, String viewInstId) {
        ListResultModel<List<FieldFormInfo>> cols = new ListResultModel();
        try {
            ViewEntityConfig config = DSMFactory.getInstance().getViewManager().getViewEntityConfig(sourceClassName, viewInstId);
            MethodConfig customMethodAPIBean = config.getSourceConfig().getMethodByName(methodName);
            CustomFormViewBean customFormViewBean = (CustomFormViewBean) customMethodAPIBean.getView();
            List<String> fieldNames = customFormViewBean.getFieldNames();
            List<FieldFormConfig> fields = new ArrayList<>();
            for (String fieldName : fieldNames) {
                FieldFormConfig fieldFormConfig = (FieldFormConfig) customFormViewBean.getFieldConfigMap().get(fieldName);
                if (fieldFormConfig != null && !fieldFormConfig.getHidden()) {
                    fieldFormConfig.setEntityClassName(customFormViewBean.getEntityClassName());

                    fields.add(fieldFormConfig);
                }
            }
            cols = PageUtil.getDefaultPageList(fields, FieldFormInfo.class);
        } catch (JDSException e) {
            e.printStackTrace();
        }
        return cols;
    }

    @RequestMapping(method = RequestMethod.POST, value = "ModuleList")
    @GridViewAnnotation
    @ModuleAnnotation(imageClass = "spafont spa-icon-c-comboinput", caption = "子模块列表")
    @APIEventAnnotation(autoRun = true)
    @ResponseBody
    public ListResultModel<List<FieldModuleView>> getModuleList(String sourceClassName, String methodName, String domainId, String viewInstId) {
        ListResultModel<List<FieldModuleView>> cols = new ListResultModel();
        try {
            ViewEntityConfig config = DSMFactory.getInstance().getViewManager().getViewEntityConfig(sourceClassName, viewInstId);
            MethodConfig customMethodAPIBean = config.getSourceConfig().getMethodByName(methodName);
            CustomFormViewBean customFormViewBean = (CustomFormViewBean) customMethodAPIBean.getView();
            List<String> fieldNames = customFormViewBean.getModuleFieldNames();
            List<FieldModuleConfig> fields = new ArrayList<>();
            for (String fieldName : fieldNames) {
                FieldModuleConfig fieldFormConfig = (FieldModuleConfig) customFormViewBean.getFieldModuleConfig(fieldName);
                if (fieldFormConfig != null) {
                    fieldFormConfig.setSourceClassName(sourceClassName);
                    fieldFormConfig.setEntityClassName(customFormViewBean.getEntityClassName());

                    fields.add(fieldFormConfig);
                }
            }
            cols = PageUtil.getDefaultPageList(fields, FieldModuleView.class);
        } catch (JDSException e) {
            e.printStackTrace();
        }
        return cols;
    }

    @RequestMapping(method = RequestMethod.POST, value = "FieldHiddenList")
    @GridViewAnnotation
    @ModuleAnnotation(imageClass = "spafont spa-icon-c-comboinput", caption = "隐藏字段")
    @APIEventAnnotation(autoRun = true)
    @ResponseBody
    public ListResultModel<List<FieldHiddenInfo>> getFieldHiddenList(String sourceClassName, String methodName, String domainId, String viewInstId) {
        ListResultModel<List<FieldHiddenInfo>> cols = new ListResultModel();
        try {
            ViewEntityConfig config = DSMFactory.getInstance().getViewManager().getViewEntityConfig(sourceClassName,  viewInstId);
            MethodConfig customMethodAPIBean = config.getSourceConfig().getMethodByName(methodName);
            CustomFormViewBean customFormViewBean = (CustomFormViewBean) customMethodAPIBean.getView();
            List<String> fieldNames = customFormViewBean.getFieldNames();
            List<FieldFormConfig> fields = new ArrayList<>();
            for (String fieldName : fieldNames) {
                FieldFormConfig fieldFormConfig = (FieldFormConfig) customFormViewBean.getFieldConfigMap().get(fieldName);
                if (fieldFormConfig != null && fieldFormConfig.getHidden()) {
                    fieldFormConfig.setEntityClassName(customFormViewBean.getEntityClassName());
                    fields.add(fieldFormConfig);
                }
            }
            cols = PageUtil.getDefaultPageList(fields, FieldHiddenInfo.class);
        } catch (JDSException e) {
            e.printStackTrace();
        }
        return cols;
    }

    @RequestMapping(method = RequestMethod.POST, value = "FormActionGroup")
    @NavButtonViewsViewAnnotation
    @DynLoadAnnotation
    @ModuleAnnotation(dock = Dock.fill, caption = "动作事件", imageClass = "spafont spa-icon-action")
    @ResponseBody
    public ResultModel<FormActionGroup> getFormActionGroup(String sourceClassName, String methodName, String domainId, String viewInstId) {
        ResultModel<FormActionGroup> result = new ResultModel<FormActionGroup>();
        return result;

    }

    public String getDomainId() {
        return domainId;
    }

    public void setDomainId(String domainId) {
        this.domainId = domainId;
    }

    public String getMethodName() {
        return methodName;
    }

    public void setMethodName(String methodName) {
        this.methodName = methodName;
    }

    public String getEsdclass() {
        return esdclass;
    }

    public void setEsdclass(String esdclass) {
        this.esdclass = esdclass;
    }

    public String getProjectId() {
        return projectId;
    }

    public void setProjectId(String projectId) {
        this.projectId = projectId;
    }

    public String getViewInstId() {
        return viewInstId;
    }

    public void setViewInstId(String viewInstId) {
        this.viewInstId = viewInstId;
    }
}
